/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import beans.SeekerCustomer;
import beans.SeekerUploadImg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 *
 * @author Dhruv
 */

//adding an image
public class AddImageAction extends Action {

    //constructor
    public AddImageAction(SeekerCustomer model) {
        super(model);
    }

    //identifies the operation name
    public String getName() {
        return "AddImage";
    }

    public Object perform(HttpServletRequest req, HttpServletResponse resp) {

        //JSP page to dispatch the request to
        String address = "";
        //use the session object to get the model object
        HttpSession session = req.getSession();
        model = (SeekerCustomer)session.getAttribute("session1");

        //if you inputted an image, make the change
        if(req.getParameter("addImage")!=null){
            address = "AddImage.jsp";
            //add image into database
            model.setImage(req.getParameter("addImage"));
            //retrieve image object from database
            SeekerUploadImg addImage = model.getImage();
        
            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("addImage", addImage);
        }
        //else, throw error message
        else{
            address="Error.jsp";
            //store attribute(s) in this request, which is(are) reset between requests
            req.setAttribute("error", "No picture inputted.");
        }
        //below object receives requests from the client and sends them to JSP address
        RequestDispatcher dispatcher = req.getRequestDispatcher(address);
        try {
            dispatcher.forward(req, resp);
        } catch (ServletException ex) {
            Logger.getLogger(CreateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
              Logger.getLogger(CreateUserAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        //returning the object conforms to the Abstract Action class definition
        //object needed to maintain session between requests
        return model;
    }
}